import os
from os import environ, path
from flask import Flask, request, jsonify, abort
import json
import requests
from config import Config
from utils.log import MyLog
from utils.redis_handler import RedisHandler
import time

app = Flask(__name__)
app.config['JSON_AS_ASCII']=False
opt = Config()
log = MyLog(opt.log_dir, __name__).getlog()
circle_red = RedisHandler(opt)


@app.route('/main_server/', methods=['GET', 'POST'])
def test():
    try:
        stime = time.time()
        if request.method == 'GET':
            group_id = request.args.get('group_id')
        elif request.method == 'POST':
            group_id = request.form['group_id']
        else:
            log.info('request error')
        result = circle_red.existed(group_id)
        log.info(f'{group_id} search finished')
        return jsonify(
            code='1',
            desc='success',
            time_consume='{} ms'.format((time.time() - stime) * 1000),
            data=result
        )
    except:
        log.info(f'search fail')
        return jsonify(
            code='0',
            desc='fail',
        )


if __name__ == '__main__':
    app.run(host="0.0.0.0", port=5000)
